package com.jkoss.wine.investment.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonMethod;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.investment.entity.Investment;
import com.jkoss.wine.investment.service.IInvestmentService;
import com.jkoss.wine.investment.vo.InvestmentImport;
import com.jkoss.wine.investment.vo.InvestmentVo;
import com.jkoss.wine.system.entity.Areas;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.MerchantEmployeesRole;
import com.jkoss.wine.system.entity.MessageNotification;
import com.jkoss.wine.system.entity.Role;
import com.jkoss.wine.system.entity.SystemSetting;
import com.jkoss.wine.system.service.IAreasService;
import com.jkoss.wine.system.service.IMerchantEmployeesRoleService;
import com.jkoss.wine.system.service.IMerchantEmployeesService;
import com.jkoss.wine.system.service.IMessageNotificationService;
import com.jkoss.wine.system.service.ISystemSettingService;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;

/**
 * 招商信息 前端控制器
 * 
 * @Author Ning
 * @Version 1.0, 2018-10-25
 * @See
 * @Since com.jkoss.wine.investment.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/investment/investment")
public class InvestmentController extends BaseController {

	@Autowired
	private IInvestmentService iInvestmentService;
	@Autowired
	private IAreasService iAreasService;
	@Autowired
	private IMerchantEmployeesService iMerchantEmployeesService;

	@Autowired
	private ISystemSettingService iSystemSettingService;
	@Autowired
	private IMessageNotificationService iMessageNotificationService;

	@Autowired
	private IMerchantEmployeesRoleService iMerchantEmployeesRoleService;

	/**
	 * 列表
	 * 
	 * @param dwzPageBean
	 * @param name
	 * @param sex
	 * @param phone
	 * @param areaCode
	 * @param state
	 * @param startTime
	 * @param endTime
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/investment/investment/list")
	public String list(String maid, DwzPageBean dwzPageBean, String name, String sex, String phone, String areaCode, String state, String startTime, String endTime, String startFwTime, String endFwTime, String intent, String today, ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String investmentname = request.getParameter("org1.name");
		String investmentphone = request.getParameter("org2.phone");
		dwzPageBean.getCountResultMap().put("name", investmentname);
		dwzPageBean.getCountResultMap().put("phone", investmentphone);
		dwzPageBean.getCountResultMap().put("startTime", startTime);
		dwzPageBean.getCountResultMap().put("endTime", endTime);
		dwzPageBean.getCountResultMap().put("startFwTime", startFwTime);
		dwzPageBean.getCountResultMap().put("endFwTime", endFwTime);
		dwzPageBean.getCountResultMap().put("maid", maid);
		dwzPageBean.getCountResultMap().put("intent", intent);
		if (!CommonUtil.isBlank(sex)) {
			dwzPageBean.getCountResultMap().put("sex", sex);
		}
		if (!CommonUtil.isBlank(state)) {
			dwzPageBean.getCountResultMap().put("state", state);
		}
		if (!CommonUtil.isBlank(today)) {
			dwzPageBean.getCountResultMap().put("today", today);
		}
		// 省市级查询
		Wrapper wrapper1 = Condition.create().like("id", "0000");
		List<Areas> provinces = iAreasService.selectList(wrapper1);
		map.put("provinces", provinces);
		String repAreaCode = "";
		if (!CommonUtil.isBlank(areaCode)) {
			String[] areas = areaCode.split(",");
			for (int i = 0; i < areas.length; i++) {
				repAreaCode += areas[i];
				if (i != 2 && areas.length - 1 > i && !CommonUtil.isBlank(areas[i + 1])) {
					repAreaCode += ",";
				}
			}
			if (areas.length >= 1 && !CommonUtil.isBlank(areas[0])) {
				dwzPageBean.getCountResultMap().put("province", areas[0]);
				if (areas.length >= 2 && !CommonUtil.isBlank(areas[0])) {
					dwzPageBean.getCountResultMap().put("city", areas[1]);
				}
				// 市
				String city = areas[0].substring(0, 2);
				Wrapper cityWrapper = Condition.create().andNew("id like '" + city + "%00'").ne("id", city + "0000");
				List<Areas> citys = iAreasService.selectList(cityWrapper);
				map.put("citys", citys);
			}
			if (areas.length >= 2 && !CommonUtil.isBlank(areas[1])) {
				if (areas.length >= 3 && !CommonUtil.isBlank(areas[0])) {
					dwzPageBean.getCountResultMap().put("county", areas[2]);
				}
				// 区
				String county = areas[1].substring(0, 4);
				Wrapper countyWrapper = Condition.create().andNew("id like '" + county + "%'").ne("id", county + "00");
				List<Areas> countys = iAreasService.selectList(countyWrapper);
				map.put("countys", countys);
			}
		}
		// wrapper条件
		Wrapper wrapper = Condition.create().ne("i.del", 1).like("name", investmentname).like("sex", sex).like("phone", investmentphone).like("area_code", repAreaCode).like("state", state);
		if (!CommonUtil.isBlank(intent)) {
			wrapper.eq("i.intent", intent);
		}
		if (!CommonUtil.isBlank(startTime)) {
			wrapper.ge("i.time", startTime);
		}
		if (!CommonUtil.isBlank(endTime)) {
			wrapper.le("i.time", CommonUtil.dateLastTime(endTime));
		}
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if (CommonUtil.isEquals("招商主管", role.getName()) || CommonUtil.isEquals("IT主管", role.getName()) || CommonUtil.isEquals("SEM专员", role.getName())) {
				isDirector = false;
				map.put("isDirector", isDirector);
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		} else {
			if (!CommonUtil.isBlank(maid)) {
				wrapper.eq("maid", maid);
			}
		}
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("add_time", false);
		}
		// 今天回访客户
		if (!CommonUtil.isBlank(today)) {
			wrapper.like("i.ntfw", CommonUtil.date());
		}
		// 跟进时间条件查询
		if (!CommonUtil.isBlank(startFwTime)) {
			wrapper.ge("i.ntfw", startFwTime);
		}
		if (!CommonUtil.isBlank(endFwTime)) {
			wrapper.le("i.ntfw", CommonUtil.dateLastTime(endFwTime));
		}

		Page resultPage = iInvestmentService.selectVoPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		// 未联系客户
		Wrapper uncWrapper = Condition.create().ne("del", 1).eq("state", 1);
		if (isDirector) {
			uncWrapper.eq("maid", getUserId());
		}
		int uncontacted = iInvestmentService.selectCount(uncWrapper);
		map.put("uncontacted", uncontacted);
		// 今日回访客户
		Wrapper todayWrapper = Condition.create().ne("del", 1).like("ntfw", CommonUtil.date());
		if (isDirector) {
			todayWrapper.eq("maid", getUserId());
		}
		int todayCount = iInvestmentService.selectCount(todayWrapper);
		map.put("todayCount", todayCount);
		map.put("today", CommonUtil.date());
		// 全部客户
		Wrapper countWrapper = Condition.create().ne("del", 1);
		if (isDirector) {
			countWrapper.eq("maid", getUserId());
		}
		int count = iInvestmentService.selectCount(countWrapper);
		map.put("count", count);

		Wrapper updatewrapper = Condition.create().eq("meid", getUserId()).eq("type", 4);
		MessageNotification entity = new MessageNotification();
		// 类型：1-通知公告未读提醒、2-周报、3-月报未读及未提交提醒、4-新客户资源分配提醒、5-资源客户未沟通提醒
		entity.setType(4);
		// 是否已读：0-未读、1-已读
		entity.setIsread(1);
		iMessageNotificationService.update(entity, updatewrapper);

		Wrapper wrapperEmployees = Condition.create().ne("del", 1);
		List<MerchantEmployees> employees = iMerchantEmployeesService.selectList(wrapperEmployees);
		map.put("employees", employees);
		return "investment/investment/list";
	}

	@RequestMapping("/orderlist")
	@RequiresPermissions("/investment/investment/orderlist")
	public String orderlist(DwzPageBean dwzPageBean, String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().eq("i.id", id);
		map.put("investments", iInvestmentService.selectVoByid(wrapper));
		return "investment/investment/orderlist";
	}

	/**
	 * 去新增
	 * 
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/investment/investment/toInsert")
	public String toInsert(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().like("id", "0000");
		List<Areas> provinces = iAreasService.selectList(wrapper);

		// 招商部角色id
		List list = new ArrayList();
		list.add("dff59b3d38f24100af209e96a18b5d13");
		list.add("5aaf79e966464793b06b17424b4f1618");
		list.add("c9990c781d6a46a3945450e27d873531");
		list.add("b342e0bc3d2149e5bdf8842486541432");

		// list.add("1729c06d9e7e40d48c51e1251ca193ff");
		// list.add("1bb036d1c9164b53b1ba3b7708b5f611");
		// list.add("224ce87ab172424ba5d8770fea861f86");
		// list.add("4cbf9b8b421e40d7b5abccaf6de63832");
		// list.add("5aaf79e966464793b06b17424b4f1618");
		// list.add("94c24bdc1e61479fae42058be8f30fa6");
		// list.add("a433148c886348d9b8e6c5d5932ae97a");
		Wrapper erwrapper = Condition.create().in("rid", list);
		List<MerchantEmployeesRole> employeesRoles = iMerchantEmployeesRoleService.selectList(erwrapper);
		List list2 = new ArrayList();
		for (MerchantEmployeesRole object : employeesRoles) {
			list2.add(object.getMeid());
		}

		// 查询招商部门员工
		Wrapper ewrapper = Condition.create().ne("del", 1).eq("dmid", "9c4a8b8541b4449e8398f1e5710cc7a9");// .in("employee_id", list2);
		List<MerchantEmployees> mEmployees = iMerchantEmployeesService.selectList(ewrapper);
		// 招商类型
		Wrapper ssWrapper = Condition.create().eq("ky", "itmt_type");
		List itmtTypes = iSystemSettingService.selectList(ssWrapper);
		map.put("itmtTypes", itmtTypes);
		map.put("provinces", provinces);
		map.put("mEmployees", mEmployees);
		return "investment/investment/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/investment/investment/toInsert")
	@ResponseBody
	public Object insert(@Valid Investment investment, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().like("phone", investment.getPhone());
		List list = iInvestmentService.selectList(wrapper);
		if (!CommonUtil.isBlank(list)) {
			return ajaxError("客户联系号码重复");
		}
		// 当前操作人
		investment.setOperator(getUserId());
		// 添加时间
		investment.setAddTime(CommonUtil.getDate());
		// 是否删除 0-未删除 1-已删除
		investment.setDel(0);
		// 状态
		investment.setState(1);
		// 默认添加人为跟进人
		investment.setMaid(getUserId());
		if (iInvestmentService.insert(investment)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/investment/investment/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper2 = Condition.create().ne("del", 1);
		List<MerchantEmployees> mEmployees = iMerchantEmployeesService.selectList(wrapper2);
		Investment investment = iInvestmentService.selectById(id);
		String string = investment.getAreaCode();
		String[] strings = string.split(",");
		String province = strings[0];
		// 省
		Wrapper wrapper = Condition.create().like("id", "0000");
		List<Areas> provinces = iAreasService.selectList(wrapper);
		map.put("provinces", provinces);
		map.put("province", province);
		// 市
		String city = province.substring(0, 2);
		Wrapper cityWrapper = Condition.create().andNew("id like '" + city + "%00'").ne("id", city + "0000");
		List<Areas> citys = iAreasService.selectList(cityWrapper);
		map.put("citys", citys);
		map.put("city", strings[1]);
		// 区
		String county = strings[1].substring(0, 4);
		Wrapper countyWrapper = Condition.create().andNew("id like '" + county + "%'");
		List<Areas> countys = iAreasService.selectList(countyWrapper);
		// 招商类型
		Wrapper ssWrapper = Condition.create().eq("ky", "itmt_type");
		List itmtTypes = iSystemSettingService.selectList(ssWrapper);
		map.put("itmtTypes", itmtTypes);
		map.put("countys", countys);
		map.put("county", strings[2]);
		map.put("mEmployees", mEmployees);
		map.put("record", iInvestmentService.selectById(id));

		// 招商主管和IT主管才有接受分配的权利，其他角色不显示
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if (CommonUtil.isEquals("招商主管", role.getName()) || CommonUtil.isEquals("IT主管", role.getName())) {
				isDirector = false;
				map.put("isDirector", isDirector);
			}
		}
		return "investment/investment/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/investment/investment/toUpdate")
	@ResponseBody
	public Object update(@Valid Investment investment, HttpServletRequest request, HttpServletResponse response) {
		if (iInvestmentService.updateById(investment)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/investment/investment/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList<>();
		for (String string : id) {
			Investment investment = new Investment();
			investment.setId(string);
			investment.setDel(1);
			list.add(investment);
		}
		if (iInvestmentService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 查询招商新客户名字
	 * 
	 * @param name
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getInvestmentName")
	@ResponseBody
	public Object getInvestmentName(String name, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("name", name).ne("del", 1);
		List<Investment> list = iInvestmentService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	@RequestMapping("/getInvestmentPhone")
	@ResponseBody
	public Object getInvestmentPhone(String phone, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("phone", phone).ne("del", 1);
		List<Investment> list = iInvestmentService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 导出EXCEL
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@RequiresPermissions("/investment/investment/export")
	public String export(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("i.del", 1);
		// 根据用户id获取角色
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("招商主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		}
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iInvestmentService.selectVoPage(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "客户信息", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, InvestmentVo.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "客户信息");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

	/**
	 * 去导入页面
	 * 
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toImportExcel")
	@RequiresPermissions("/investment/investment/toImportExcel")
	public String toImportExcel(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 招商类型
		Wrapper ssWrapper = Condition.create().eq("ky", "itmt_type");
		List itmtTypes = iSystemSettingService.selectList(ssWrapper);
		map.put("itmtTypes", itmtTypes);
		return "investment/investment/importExcel";
	}

	/**
	 * 导入EXCEL
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/importExcel")
	@RequiresPermissions("/investment/investment/toImportExcel")
	@ResponseBody
	public Object importExcel(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 1.构造导入参数
		ImportParams params = new ImportParams();
		// 指定title位置
		// 2.importExcelMore解释Excel
		ExcelImportResult<InvestmentImport> result = ExcelImportUtil.importExcelMore(file.getInputStream(), InvestmentImport.class, params);
		// 3.插入数据库
		List<InvestmentImport> investmentVos = result.getList();
		if (!CommonUtil.isBlank(investmentVos)) {
			// 批量插入去重
			List list = new ArrayList();
			for (InvestmentImport investmentImport : investmentVos) {
				list.add(investmentImport.getPhone());
			}
			Wrapper itWrapper = Condition.create().in("phone", list);
			List<Investment> investments2 = iInvestmentService.selectList(itWrapper);

			List list2 = new ArrayList();
			for (Investment investment : investments2) {
				list2.add(investment.getPhone());
			}

			if (!CommonUtil.isBlank(list2)) {
				throw new Exception("手机号码已存在" + JSON.toJSONString(list2));
			}

			// 为提高效率一次查询所有用户，放到map当中
			Map<String, String> umap = new HashMap();
			List<MerchantEmployees> merchantEmployees = iMerchantEmployeesService.selectList(null);
			if (!CommonUtil.isBlank(merchantEmployees)) {
				for (MerchantEmployees user : merchantEmployees) {
					umap.put(user.getEmployeeName(), user.getEmployeeId());
				}
			}

			// 为提高效率一次查询所有招商类型，放到map当中
			Map<String, String> itmap = new HashMap();
			Wrapper ssWrapper = Condition.create().eq("ky", "itmt_type");
			List<SystemSetting> systemSettings = iSystemSettingService.selectList(ssWrapper);
			if (!CommonUtil.isBlank(systemSettings)) {
				for (SystemSetting systemSetting : systemSettings) {
					itmap.put(systemSetting.getVal(), systemSetting.getId());
				}
			}

			List<Investment> investments = new ArrayList();
			for (InvestmentImport investmentVo : investmentVos) {

				if (CommonUtil.isBlank(investmentVo.getName())) {
					throw new Exception("客户不能为空");
				}
				if (CommonUtil.isBlank(investmentVo.getDate())) {
					throw new Exception("日期不能为空");
				}
				if (CommonUtil.isBlank(investmentVo.getTime())) {
					throw new Exception("时间不能为空");
				}
				if (CommonUtil.isBlank(investmentVo.getPhone())) {
					throw new Exception("手机不能为空");
				}
				if (CommonUtil.isBlank(investmentVo.getAreaName())) {
					throw new Exception("地区不能为空");
				}
				if (CommonUtil.isBlank(investmentVo.getSource())) {
					throw new Exception("来源不能为空");
				}
				if (investmentVo.getAreaName().split(",").length != 3) {
					throw new Exception("地区格式错误");
				}

				// 把InvestmentVo转换成Investment
				// Investment investment = CommonUtil.cloneJavaBean(investmentVo,
				// Investment.class);
				Investment investment = new Investment();
				investment.setName(investmentVo.getName());
				investment.setPhone(investmentVo.getPhone());
				investment.setDate(investmentVo.getDate());
				investment.setTime(CommonMethod.iso6Format.parse(CommonMethod.isoFormat.format(investmentVo.getDate()) + " " + CommonMethod.timeFormat.format(investmentVo.getTime())));
				investment.setOther(investmentVo.getOther());
				investment.setSource(investmentVo.getSource());
				investment.setRemk1(investmentVo.getRemk1());
				investment.setRemk2(investmentVo.getRemk2());
				investment.setAreaName(investmentVo.getAreaName());
				
				investment.setState(investmentVo.getState());
				// 操作人
				investment.setOperator(getUserId());
				// 添加时间
				investment.setAddTime(CommonUtil.getDate());
				// 是否删除：0-未删除、1-已删除、2-不能删除
				investment.setDel(0);

				if (!CommonUtil.isBlank(umap.get(investmentVo.getMename()))) {
					// 跟进人id转换
					investment.setMaid(umap.get(investmentVo.getMename()));
				} else {
					// 没有填写跟进人，默认导入人
					investment.setMaid(getUserId());
				}

				// 设置地区编号
				String areaname = investmentVo.getAreaName();
				if (!CommonUtil.isBlank(areaname)) {
					String[] areanames = areaname.split(",");
					String code = "";
					for (int i = 0; i < areanames.length; i++) {
						Wrapper wrapper = Condition.create().eq("name", areanames[i]);
						Areas areas = iAreasService.selectOne(wrapper);
						code += areas.getId();
						if (i != areanames.length - 1) {
							code += ",";
						}
					}
					investment.setAreaCode(code);
				}
				investments.add(investment);
			}
			if (!iInvestmentService.insertBatch(investments)) {
				return ajaxError("添加数据失败！");
			}
		} else {
			return ajaxError("数据内容为空！");
		}
		return ajaxSuccess();// View名称}
	}

	/**
	 * 去批量修改跟进人
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdateMaid")
	@RequiresPermissions("/investment/investment/toUpdateMaid")
	public String toUpdateMeid(String ids, ModelMap map) {
		// 【招商部门】除部门主管外，其他任何本部门下辖岗位不显示跟进人选项，其默认为帐号本人。
		List list = new ArrayList();
		// 招商部门
		list.add("dff59b3d38f24100af209e96a18b5d13");
		list.add("5aaf79e966464793b06b17424b4f1618");
		list.add("c9990c781d6a46a3945450e27d873531");
		list.add("b342e0bc3d2149e5bdf8842486541432");

		// 其他部门主管
		// list.add("1729c06d9e7e40d48c51e1251ca193ff");
		// list.add("1bb036d1c9164b53b1ba3b7708b5f611");
		// list.add("224ce87ab172424ba5d8770fea861f86");
		// list.add("4cbf9b8b421e40d7b5abccaf6de63832");
		// list.add("5aaf79e966464793b06b17424b4f1618");
		// list.add("94c24bdc1e61479fae42058be8f30fa6");
		// list.add("a433148c886348d9b8e6c5d5932ae97a");
		Wrapper erwrapper = Condition.create().in("rid", list);
		List<MerchantEmployeesRole> employeesRoles = iMerchantEmployeesRoleService.selectList(erwrapper);
		List list2 = new ArrayList();
		for (MerchantEmployeesRole object : employeesRoles) {
			list2.add(object.getMeid());
		}
		Wrapper ewrapper = Condition.create().in("employee_id", list2).ne("del", 1);
		List<MerchantEmployees> mEmployees = iMerchantEmployeesService.selectList(ewrapper);
		map.put("employees", mEmployees);
		map.put("ids", ids);
		return "investment/investment/updateMaid";
	}

	/**
	 * 批量修改跟进人
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/updateMaid")
	@RequiresPermissions("/investment/investment/toUpdateMaid")
	@ResponseBody
	public Object updateMaid(String ids, String maid, HttpServletRequest request, HttpServletResponse response) {
		if (iInvestmentService.updateMaid(ids, maid)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

}
